﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;

namespace LinqDll
{
    /// <summary>
    ///     这个类只能在BLL层中使用,如果你直接在其他层中使用,我只想说你个sb连三层架构你都不懂吗?
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LinqWrapper<T>
        where T : class, new()
    {
        #region Members

        private DataContext _db;

        public LinqWrapper(DataContext db)
        {
            if (db == null)
                throw new ArgumentNullException();

            _db = db;
        }

        public DataContext Db
        {
            get { return _db; }
            set
            {
                if (value != _db)
                    _db = value;
            }
        }

        public Table<T> Table
        {
            get { return Db.GetTable<T>(); }
        }
        #endregion

        #region method

        

        public void CreateList(IEnumerable<T> ts)
        {
            Table.InsertAllOnSubmit(ts);
            Db.SubmitChanges();
        }

        public void Create(T t)
        {
            Table.InsertOnSubmit(t);
            Db.SubmitChanges();
        }

        public void Delete(T t)
        {
            Table.DeleteOnSubmit(t);
            Db.SubmitChanges();
        }

        public void Update(T t)
        {
            try
            {
                Table.Attach(t);
                Db.Refresh(RefreshMode.KeepCurrentValues, t);
            }
            catch (Exception)
            {
            }
            finally
            {
                Db.SubmitChanges();
            }
        }

        public void Dispose()
        {
            Db.Dispose();
        }

        public void Delete(IEnumerable<T> ts)
        {
            Table.DeleteAllOnSubmit(ts);
            Db.SubmitChanges();
        }

        public void Update(IEnumerable<T> ts)
        {
            try
            {
                Table.AttachAll(ts);
                Db.Refresh(RefreshMode.KeepCurrentValues, ts);
            }
            catch (Exception)
            {
            }
            finally
            {
                Db.SubmitChanges();
            }
        }

        public T FirstOrDefault()
        {
            return Table.FirstOrDefault();
        }

        public T Query(Func<T, bool> func)
        {
            return Table.FirstOrDefault(func);
        }

        public IQueryable<T> QueryList()
        {
            return Table.AsQueryable();
        }

        public IQueryable<T> QueryList(Func<T, bool> func)
        {
            if (func == null)
                return Table.AsQueryable();
            return Table.Where(func).AsQueryable();
        }

        public IQueryable<T> QueryList(Func<T, int, bool> func)
        {
            if (func == null)
                return Table.AsQueryable();
            return Table.Where(func).AsQueryable();
        }

        public IQueryable<T> Pager(int skipNum, int takeNum)
        {
            return Table.Skip(skipNum).Take(takeNum);
        }

        #endregion
    }
}